using Microsoft.AspNetCore.Mvc;
using Common.Helper;
using test.Models;

namespace test.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SettingController : ControllerBase
    {
        private readonly hongjiContext _db;
        public SettingController(hongjiContext db)
        {
            _db = db;
        }

        [HttpGet(Name = "SettingID")]
        public Setting Get(int ID = 1)
        {
            Setting S = new();
            var s = _db.Settings.FirstOrDefault(x => x.Id == ID);
            if (s != null)
            {
                S = s;
            }
            return S;
        }


        [HttpPost(Name = "InsertSettingList")]
        public bool Post(Setting S)
        {
            var s = _db.Settings.FirstOrDefault(x => x.Id == S.Id);
            if (s == null)
            {
                Setting s1 = new()
                {
                    Seotitle = S.Seotitle,
                    Title = S.Title,
                    Keywords = S.Keywords,
                    Description = S.Description,
                    CompanyName = S.CompanyName,
                };
                _db.Settings.Add(s1);
            }
            else
            {
                s.Seotitle = S.Seotitle;
                s.Title = S.Title;
                s.Keywords = S.Keywords;
                s.Description = S.Description;
                s.CompanyName = S.CompanyName;
                _db.Settings.Attach(s);
            }

            LogHelper.Write("Invoke Post");
            if (_db.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

    }
}